package com.sixth.volunteerservicesystem.Controller.theoryback;

import com.sixth.volunteerservicesystem.Service.LearnTheoryservice;
import com.sixth.volunteerservicesystem.Service.Videoservice;
import com.sixth.volunteerservicesystem.domain.Theory;
import com.sixth.volunteerservicesystem.domain.Video;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class learntheory {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Autowired
    LearnTheoryservice learnTheoryservice;
    @Autowired
    Videoservice videoservice;
    @RequestMapping("/index")
    public String index() {
        return "index";
    }


    @GetMapping("/learntheory")
    public String list(Model model) {
        List<Theory> theory = learnTheoryservice.list();
        theory.forEach(System.out::println);
        List<Video> videos = videoservice.list();
        model.addAttribute("videos", videos);
        model.addAttribute("theory", theory);
        return "learntheory/learntheory";
    }

    @GetMapping("/theorydetail/{id}")
    public String theorydetail(@PathVariable("id")int id, Model model) {
        Theory theory = learnTheoryservice.getone(id);
        int view=theory.getViewcount()+1;
        String sql1 = "update theory set viewcount=? where id=?";
        Object[] args1 = {view, theory.getId()};
        jdbcTemplate.update(sql1, args1);
        model.addAttribute("theory",theory);
        return "redirect:/return/"+id;
    }
    @GetMapping("/upquantity/{id}")
    public String upquantity(@PathVariable("id")int id, Model model) {
        Theory theory = learnTheoryservice.getone(id);
        int up=theory.getThumbupquantity()+1;
        String sql = "update theory set thumbupquantity=? where id=?";
        Object[] args = {up, theory.getId()};
        jdbcTemplate.update(sql, args );
        model.addAttribute("theory",theory);
        return "redirect:/return/"+id;
    }
    @GetMapping("/return/{id}")
    public  String retur(Model model, @PathVariable("id")int id){
        Theory theory = learnTheoryservice.getone(id);
        model.addAttribute("theory",theory);
        return "learntheory/theorydetail";
    }
}